package com.mall.manager.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mall.untils.DateUtils;
import com.mall.common.dao.MongoDBManager;
import com.mall.common.dao.MysqlDBManager;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

@Service
public class SingleTopicDayService {
	@Resource
    private JdbcTemplate jdbcTemplate;
	//日志信息
	private Logger logger = LoggerFactory.getLogger(SingleTopicDayService.class);	
	/**
	 * 查询单题sdk_collection 统计测试时间
	 * @return
	 */
	public List<String> findAllBySdk() {
//		List<DBObject> findAll = MongoDBManager.findAll("sdk_collection");
		List<String> stingList = new ArrayList<String>();
		try {
			//当前日期的前一天
			String beforDate = DateUtils.getNowBeforDay();
			//按时间检索前一天的数据
			BasicDBObject dbo = new BasicDBObject();
			dbo.append("commit_time", new BasicDBObject("$gte", beforDate+" 00:00:00"));
			dbo.append("commit_time", new BasicDBObject("$lte", beforDate+" 23:59:59"));
			BasicDBList list = new BasicDBList();
			list.add(dbo);
			BasicDBObject d = new BasicDBObject();
			d.put("$and", list);
			List<DBObject> findAll = MongoDBManager.findForCommitTime("sdk_collection", d);
			
			String res3;
			for (DBObject dbObject : findAll) {			
				String string = String.valueOf(dbObject.get("t_id"));             //题目id
				String string1 = String.valueOf(dbObject.get("t_time"));          //题测时长
				String string2 = String.valueOf(dbObject.get("baby_id"));         //babt的id
				String string3 = String.valueOf(dbObject.get("data_type"));       //业务类型
				if(!string.equals("null")){				
				   res3 = string+","+string1+","+string2 + "," +string3;
				   stingList.add(res3);
				}
			}		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		
		return stingList;
	}	
	/**
	 * 查询单题pc_collection 统计测试时间
	 * @return
	 */
	public List<String> findAllByPc() {
		List<String> stingList = new ArrayList<String>();
		try {
			String beforDate = DateUtils.getNowBeforDay();
			//按时间检索前一天的数据
			BasicDBObject dbo = new BasicDBObject();
			dbo.append("commit_time", new BasicDBObject("$gte", beforDate+" 00:00:00"));
			dbo.append("commit_time", new BasicDBObject("$lte", beforDate+" 23:59:59"));
			BasicDBList list = new BasicDBList();
			list.add(dbo);
			BasicDBObject d = new BasicDBObject();
			d.put("$and", list);
			List<DBObject> findAll = MongoDBManager.findForCommitTime("pc_collection", d);
			
			String res3;
			for (DBObject dbObject : findAll) {			
				String string = String.valueOf(dbObject.get("t_id"));             //题目id
				String string1 = String.valueOf(dbObject.get("t_time"));          //题测时长
				String string2 = String.valueOf(dbObject.get("baby_id"));         //babt的id
				String string3 = String.valueOf(dbObject.get("data_type"));       //业务类型
				if(!string.equals("null")){				
				   res3 = string+","+string1+","+string2 + "," +string3;
				   stingList.add(res3);
				}
			}		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.info(e.getMessage());
		}		
		return stingList;
	}
	/**
	 * 查询单题html_collection 统计测试时间
	 * @return
	 */
	public List<String> findAllByHtml() {
		List<String> stingList = new ArrayList<String>();
		try {
			String beforDate = DateUtils.getNowBeforDay();
			//按时间检索前一天的数据
			BasicDBObject dbo = new BasicDBObject();
			dbo.append("commit_time", new BasicDBObject("$gte", beforDate+" 00:00:00"));
			dbo.append("commit_time", new BasicDBObject("$lte", beforDate+" 23:59:59"));
			BasicDBList list = new BasicDBList();
			list.add(dbo);
			BasicDBObject d = new BasicDBObject();
			d.put("$and", list);
			List<DBObject> findAll = MongoDBManager.findForCommitTime("html_collection", d);
			
			String res3;
			for (DBObject dbObject : findAll) {			
				String string = String.valueOf(dbObject.get("t_id"));             //题目id
				String string1 = String.valueOf(dbObject.get("t_time"));          //题测时长
				String string2 = String.valueOf(dbObject.get("baby_id"));         //babt的id
				String string3 = String.valueOf(dbObject.get("data_type"));       //业务类型
				if(!string.equals("null")){				
				   res3 = string+","+string1+","+string2 + "," +string3;
				   stingList.add(res3);
				}
			}		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}		
		return stingList;
	}	
	/**
	 * 统计测试时间
	 * @param topic_id  题目id
	 * @param count     题目出现次数
	 * @param avgtime   平均答题时间（秒）
	 */
	public static void manager_insert_singleTopicDay(int topic_id,int count,int avgtime){
		Connection conn = null;
		PreparedStatement ps = null;
		try {			
			String sql="insert into t_single_topic_day(topic_id,topic_sum,topic_avgtime,create_time,update_time) values(?,?,?,?,?)";
			conn = MysqlDBManager.getConnection();
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps.setInt(1, topic_id);
			ps.setInt(2, count);
			ps.setInt(3, avgtime);
			ps.setTimestamp(4, DateUtils.getNowBeforTimestamp());
			ps.setTimestamp(5, DateUtils.getTimestamp());
			ps.addBatch();
			ps.executeBatch();
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}	
	/**
     * @单题按天统计表信息增加
     * @param paraMap
     * @return
     */
	public int manager_insert_singleTopicDay(Map<String, Object> paraMap) {
		String topic_sum = MapUtils.getString(paraMap, "topic_sum");
		String topic_avgtime = MapUtils.getString(paraMap, "topic _avgtime");
		try {
			String sql="insert into t_users(id,topic_id,topic_sum,topic_avgtime,create_time,update_time) values(?,?,?,?,now(),now())";
			int update = jdbcTemplate.update(sql,topic_sum,topic_avgtime);
			return update;
		} catch (Exception e) {
			logger.debug(e.getMessage());
			return 0;
		}
	}
}
